package com.dubbo.service.impl;

import com.dubbo.common.ResponseUtil;
import com.dubbo.common.page.PageResponse;
import com.dubbo.common.page.PageUtils;
import com.dubbo.common.result.ResultInfo;
import com.dubbo.domain.auth.Area;
import com.dubbo.mapper.auth.AreaMapper;
import com.dubbo.service.auth.AreaService;
import com.dubbo.service.impl.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * 地区
 * @author lihongwu
 * 2019年1月7日 下午4:42:50
 */
@Service
@Transactional(readOnly=false,rollbackFor=Exception.class)
public class AreaServiceImpl extends BaseServiceImpl<Area> implements AreaService {

	@Autowired
	private AreaMapper areaMapper;
	/**
	 * 获取省列表
	 *
	 * @author lihongwu
	 * 2019年1月7日 下午4:46:52
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 */
	@Override
	public Map<String, Object> getProvinceList(Long pageStart, Long pageTotal) {
		pageStart = PageUtils.initialPageStart(pageStart);
		pageTotal = PageUtils.packagePageTotal(pageTotal, 5, 10);
		long countPageStart = PageUtils.countPageStart(pageStart, pageTotal);
		List<Map<String,Object>> data = areaMapper.getProvinceList(countPageStart,pageTotal);
		Long pageCount = areaMapper.getProvinceListCount();
		return ResponseUtil.ajaxCheckSuccess(ResultInfo.OK,new PageResponse(pageStart, pageTotal, pageCount, data));
	}
	
	/**
	 * 根据parentId获取下级列表
	 *
	 * @author lihongwu
	 * 2019年1月7日 下午4:59:16
	 * @param parentId
	 * @param pageStart
	 * @param pageTotal
	 * @return
	 */
	@Override
	public Map<String, Object> getSubordinateList(Long parentId, Long pageStart, Long pageTotal) {
		if(parentId==null) {
			return ResponseUtil.ajaxCheckError(ResultInfo.SYSTEM_ERROR, "parentId is null");
		}
		pageStart = PageUtils.initialPageStart(pageStart);
		pageTotal = PageUtils.packagePageTotal(pageTotal, 5, 10);
		long countPageStart = PageUtils.countPageStart(pageStart, pageTotal);
		List<Map<String,Object>> data = areaMapper.getSubordinateList(parentId,countPageStart,pageTotal);
		Long pageCount = areaMapper.getSubordinateListCount(parentId);
		return ResponseUtil.ajaxCheckSuccess(ResultInfo.OK,new PageResponse(pageStart, pageTotal, pageCount, data));
	}
	
	
}
